//---------------------------------------------------------------------------

#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include <eic.h>

#include "uDessin.h"
#include "Printer.h"
#include "uCerfa.h"
#include "uFncGDI.h"
#pragma package(smart_init)

#define MODULE_TAG  MODULE_TAG_CERFA

#define COEF_Y 0.98591549
#define MAX_ARG 6
#define RESOL_REF_CERFA 300.
//1440

#include "s:\autres\editeur_de_cerfas\declar.h"


//---------------------------------------------------------------------------
__fastcall TCerfa::TCerfa( const AnsiString &File)
{
    m_hFont = m_hOldFont = m_hFontEcran = m_hOldFontEcran = 0;
    m_File = File;
    m_hCerfaFile = 0;
}

//---------------------------------------------------------------------------
__fastcall TCerfa::~TCerfa( )
{
}

//---------------------------------------------------------------------------
#define GetFilePointer(hFile) SetFilePointer(hFile, 0, NULL, FILE_CURRENT)
void __fastcall TCerfa::ReadFile( LPVOID lpBuffer, DWORD nNumberOfBytesToRead)
{
    DWORD n;
    if( !::ReadFile( m_hCerfaFile, lpBuffer, nNumberOfBytesToRead, &n, NULL) ||
        (n  != nNumberOfBytesToRead) ) {
        throw ELastError( TAG( 160));
    }
    n = ::GetFilePointer(m_hCerfaFile);
    m_CerfaFileEOF = (::GetFilePointer(m_hCerfaFile) == ::GetFileSize( m_hCerfaFile, NULL));
}
//---------------------------------------------------------------------------

void __fastcall TCerfa::ReadArg( int Num, short *pnArg, int nLongArg, long *plArg, int nStrArg, char **ppStrArg, char *szBuf, int nSizeOfBuf)
{
    if( nStrArg == Num)
    { // L'argument n° 1 est une chaîne
        short nSizeOfStr;
        ReadFile( &nSizeOfStr, sizeof( nSizeOfStr));
        if( nSizeOfStr > 0)
        {
            ASSERT( nSizeOfStr <= nSizeOfBuf);
            memset( szBuf, 0, nSizeOfBuf);
            ReadFile( (*ppStrArg) = szBuf, nSizeOfStr);
        }
        else
        {
            ASSERT( nSizeOfStr == 0);
            (*ppStrArg) = NULL;
        }
    }
    else if(nLongArg == Num)
    {
        ReadFile( plArg, sizeof( long));
    }
    else
    {
        ReadFile( pnArg, sizeof( short));
    }
}

//---------------------------------------------------------------------------
void TCerfa::Draw( TCanvas *canvas, int nPage, const TCoorRect &Rc, const TCoorPoint *PtOrigin)
{
    TCoorRect          cr;
    enum CODE_FONCTION eCodeFonction = FCT_FIN_PAGE;
    int                nNbArgs;
    short              shArg[ MAX_ARG];
    int                nStrArg;
    int                nLongArg = 0;
    long               lArg[ MAX_ARG];
    char*              pStrArg;
    int                nNoPage;
    char               szBuf[ 250 + 1];
    TFont              *OldFont = new TFont();
    m_Rc = Rc;
    m_Rc.Top.PixelsY  = m_Rc.Top.PixelsY;
    m_Rc.Left.PixelsX = m_Rc.Left.PixelsX;
    if( PtOrigin) m_PtOrigin = *PtOrigin;
    else          {m_PtOrigin.x = 0; m_PtOrigin.y = 0;}
    m_CerfaFileEOF = false;
    m_hCerfaFile = ::CreateFile( m_File.c_str(), GENERIC_READ, FILE_SHARE_READ,
                                 NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
    if( m_hCerfaFile == INVALID_HANDLE_VALUE)
    {
        AfficheLastError();
        return;
    }
    try{try{
        m_hDCRefCerfa = ::_CreateDC( TAGL, "DISPLAY", NULL, NULL, NULL);
        ::_SetMapMode( TAGL, m_hDCRefCerfa, MM_ANISOTROPIC);
        ChkSDK( TAGL, ::SetWindowExtEx( m_hDCRefCerfa, RESOL_REF_CERFA, RESOL_REF_CERFA, NULL), false);
        double dX = Glob->ResolEcran,//::_GetDeviceCaps(TAGL, m_hDCRefCerfa, LOGPIXELSX),
               dY = Glob->ResolEcran;//::_GetDeviceCaps(TAGL, m_hDCRefCerfa, LOGPIXELSX);
        ChkSDK( TAGL, ::SetViewportExtEx(m_hDCRefCerfa, (int)floor( dX),(int)floor(dY), NULL), false);

        m_Canvas = canvas;

        m_Canvas->Brush->Color = clWhite;
        m_Canvas->Brush->Style = bsSolid;
        if( Glob->bConception)
            m_Canvas->FillRect( Rc.RectPixels);

        m_Canvas->Pen->Style = psSolid;
        m_Canvas->Pen->Color = clBlack;

        m_xDroit = 0;
        m_nLastSizeDeciPoint = 74;
        m_Gauche = CX(0);

        OldFont->Assign( m_Canvas->Font);
        BlackText();
        GetFont( &m_lf);
        m_lf.lfHeight = -11;
        m_lf.lfWidth = 0;
        m_lf.lfEscapement = 0;
        m_lf.lfOrientation = 0;
        m_lf.lfWeight = FW_NORMAL;
        m_lf.lfItalic = false;
        m_lf.lfUnderline = false;
        m_lf.lfStrikeOut = false;
        SetFont( &m_lf);
        ::_SetTextAlign( TAGL, m_Canvas->Handle, TA_LEFT + TA_BASELINE);

        m_ListOfPushedFont = new TList();
        m_ListOfPushedSizeDeciPoint = new TList();
        ::SetFilePointer( m_hCerfaFile, 0, NULL, FILE_BEGIN);
        nNoPage = 1;

        while(!m_CerfaFileEOF)
        {
            short s;
            //A voir, si encore utile
            if( (eCodeFonction == FCT_FIN_PAGE) || (eCodeFonction == FCT_FIN_PAGE_PAYSAGE))
            {
                if( nNoPage != 1)
                    cr.Top = 0;
            }

            ReadFile( &s, sizeof( s));

            eCodeFonction = (CODE_FONCTION)s;

            #if FCT_MAX != 28
                #error "eCodeFonction non reconnue"
            #endif
            switch( eCodeFonction)
            {   case FCT_TEXT_OUT         : nNbArgs = 3; nStrArg = 3;               break;
                case FCT_CAPITAL_OUT      : nNbArgs = 5; nStrArg = 5; nLongArg = 0; break;
                case FCT_FONT_STYLE       : nNbArgs = 1; nStrArg = 0;               break;
                case FCT_FONT_SIZE        : nNbArgs = 1; nStrArg = 0;               break;
                case FCT_FONT_OR          : nNbArgs = 1; nStrArg = 0;               break;
                case FCT_FONT_ITALIC      : nNbArgs = 1; nStrArg = 0;               break;
                case FCT_FONT_WEIGHT      : nNbArgs = 1; nStrArg = 0;               break;
                case FCT_CADRE            : nNbArgs = 4; nStrArg = 0;               break;
                case FCT_RECTANGLE        : nNbArgs = 5; nStrArg = 0; nLongArg = 0; break;
                case FCT_WHITE_TEXT       : nNbArgs = 0; nStrArg = 0;               break;
                case FCT_BLACK_TEXT       : nNbArgs = 0; nStrArg = 0;               break;
                case FCT_CADRE_ADM        : nNbArgs = 5; nStrArg = 0; nLongArg = 0; break;
                case FCT_DOTTED_LINE      : nNbArgs = 4; nStrArg = 0;               break;
                case FCT_V_LINE           : nNbArgs = 3; nStrArg = 0;               break;
                case FCT_H_LINE           : nNbArgs = 3; nStrArg = 0;               break;
                case FCT_BMP_OUT          : nNbArgs = 4; nStrArg = 0;               break;
                case FCT_CASE_A_COCHER    : nNbArgs = 3; nStrArg = 3;               break;
                case FCT_FIN_PAGE         : nNbArgs = 0; nStrArg = 0;               break;
                case FCT_FIN_PAGE_PAYSAGE : nNbArgs = 0; nStrArg = 0;               break;
                case FCT_FLECHE_OUT       : nNbArgs = 5; nStrArg = 0; nLongArg = 0; break;
                case FCT_PASTILLE_OUT     : nNbArgs = 4; nStrArg = 3;               break;
                case FCT_TEXT_OUT_VERTICAL: nNbArgs = 3; nStrArg = 3;               break;
                case FCT_PANNEAU          : nNbArgs = 5; nStrArg = 0; nLongArg = 0; break;
                case FCT_TEXT_OUT_R       : nNbArgs = 4; nStrArg = 3;               break;
                case FCT_COLOR_TEXT_BK    : nNbArgs = 2; nStrArg = 0;               break;
                case FCT_BMP_OUT_EX       : nNbArgs = 5; nStrArg = 0; nLongArg = 5; break;
                case FCT_BMP_OUT_GRIS     : nNbArgs = 4; nStrArg = 0;               break;
                case FCT_BMP_OUT_INV      : nNbArgs = 4; nStrArg = 0;               break;
                case FCT_TEXT_OUT_BIAIS   : nNbArgs = 4; nStrArg = 4;               break;
                default                   : nNbArgs =-1; ASSERT( FALSE);            break;
            }
            if( nNbArgs < 0)
            { // Erreur: code de fonction inconnu
                TRACE( "CDeclaration::ExecuteDescFile() => Code de fonction inconnu\n");
                break;
            }

            // Lecture de l'argument n° 1 //
            if( nNbArgs >= 1)    ReadArg( 1, &shArg[0], nLongArg, &lArg[0], nStrArg, &pStrArg, szBuf, sizeof( szBuf));
            // Lecture de l'argument n° 2
            if( nNbArgs >= 2)    ReadArg( 2, &shArg[1], nLongArg, &lArg[1], nStrArg, &pStrArg, szBuf, sizeof( szBuf));
            // Lecture de l'argument n° 3
            if( nNbArgs >= 3)    ReadArg( 3, &shArg[2], nLongArg, &lArg[2], nStrArg, &pStrArg, szBuf, sizeof( szBuf));
            // Lecture de l'argument n° 4
            if( nNbArgs >= 4)    ReadArg( 4, &shArg[3], nLongArg, &lArg[3], nStrArg, &pStrArg, szBuf, sizeof( szBuf));
            // Lecture de l'argument n° 5
            if( nNbArgs >= 5)    ReadArg( 5, &shArg[4], nLongArg, &lArg[4], nStrArg, &pStrArg, szBuf, sizeof( szBuf));
            // Lecture de l'argument n° 5
            if( nNbArgs >= 6)    ReadArg( 6, &shArg[5], nLongArg, &lArg[5], nStrArg, &pStrArg, szBuf, sizeof( szBuf));

            //Ajout pour prendre en compte le num page passe en param
            if (nNoPage != nPage) {
                if ((eCodeFonction == FCT_FIN_PAGE) || (eCodeFonction == FCT_FIN_PAGE_PAYSAGE)) {
                    ReadFields( );
                    nNoPage++;
                }
                //Si image il faut se placer apres l'image
                if ((eCodeFonction == FCT_BMP_OUT) || (eCodeFonction == FCT_BMP_OUT_GRIS)
                    || (eCodeFonction == FCT_BMP_OUT_EX) || (eCodeFonction == FCT_BMP_OUT_INV)) {
                    int nBitmapSize  = shArg[ 2] *  shArg[ 3];
                    char *aBits = (char *)::_GlobalAlloc(TAGL, GPTR, nBitmapSize);
                    ReadFile( aBits, nBitmapSize);
                    ::_GlobalFree(TAGL, aBits);
                }
                continue;
            }

            // Exécution de la fonction //
            #if FCT_MAX != 28
                #error "eCodeFonction non reconnue"
            #endif
            switch( eCodeFonction)
            {
                case FCT_TEXT_OUT_R       :
                    TextOutReduit( shArg[ 0], shArg[ 1], pStrArg, shArg[ 3]);
                break;
                case FCT_TEXT_OUT         :
                    TextOut( shArg[ 0], shArg[ 1], pStrArg);
                break;
                case FCT_CAPITAL_OUT      :
                    CapitalOut( shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3], pStrArg);
                break;
                case FCT_FONT_STYLE       :
                    FontStyle( (enum FONT_STYLE)shArg[ 0]);
                break;
                case FCT_FONT_SIZE        :
                    FontSize( shArg[ 0]);
                break;
                case FCT_FONT_OR          : //FontOrientation( nArg1);
                    ASSERT( false);
                break;
                case FCT_FONT_ITALIC      :
                    FontItalic( shArg[ 0]);
                break;
                case FCT_FONT_WEIGHT      :
                    FontWeight( shArg[ 0]);
                break;
                case FCT_CADRE            :
                    Cadre( shArg[ 0],  shArg[ 1], shArg[ 2], shArg[ 3]);
                break;
                case FCT_RECTANGLE        :
                    Rectangle( shArg[ 0],  shArg[ 1], shArg[ 2], shArg[ 3], (enum GRISE_DE_RECTANGLE)shArg[ 4]);
                break;
                case FCT_WHITE_TEXT       : WhiteText();
                break;
                case FCT_BLACK_TEXT       : BlackText();
                break;
                case FCT_COLOR_TEXT_BK    : ColorTextAndBk((enum GRISE_DE_RECTANGLE)shArg[ 0], (enum GRISE_DE_RECTANGLE)shArg[ 1]);
                break;
                case FCT_CADRE_ADM        : //On n'utilise pas les cadres ADM du Cerfas
                break;
                case FCT_DOTTED_LINE      :
                    DottedLine( shArg[ 0], shArg[ 1], shArg[ 2], (enum DOTTED_LINE_STYLE)shArg[ 3]);
                break;
                case FCT_V_LINE           :
                    VLine( shArg[ 0], shArg[ 1], shArg[ 2]);
                break;
                case FCT_H_LINE           :
                    HLine( shArg[ 0], shArg[ 1], shArg[ 2]);
                break;
                case FCT_BMP_OUT          : //On ignore la BitMap (nArg1/2 = x/Y, nArg3/4 = NbOctetsLarg/NbLignes)
                    BmpOut(shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3]);
                break;
                case FCT_BMP_OUT_EX       :
                    BmpOutEx(shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3], lArg[4]);
                break;
                case FCT_BMP_OUT_GRIS     :
                    BmpOutGrisDur(shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3]);
                break;
                case FCT_BMP_OUT_INV      :
                    BmpOutInverse(shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3]);
                break;
                case FCT_CASE_A_COCHER    :
                    CaseACocher( shArg[ 0], shArg[ 1], pStrArg);
                break;
                case FCT_FLECHE_OUT       :
                    FlecheOut( shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3], shArg[ 4]);
                break;
                case FCT_PASTILLE_OUT     :
                    PastilleOut( shArg[ 0], shArg[ 1], pStrArg, shArg[ 3]);
                break;
                case FCT_TEXT_OUT_VERTICAL:
                    TextOutV( shArg[ 0], shArg[ 1], pStrArg);
                break;
                case FCT_TEXT_OUT_BIAIS:
                    TextOutBiais( shArg[ 0], shArg[ 1], pStrArg);
                break;
                case FCT_PANNEAU          :
                    Panneau( shArg[ 0], shArg[ 1], shArg[ 2], shArg[ 3], shArg[ 4]);
                break;
                case FCT_FIN_PAGE         :
                    ReadFields( );
                    nNoPage++;
                break;
                case FCT_FIN_PAGE_PAYSAGE :
                    ReadFields( );
                    nNoPage++;
                break;

                default                   : break;
            }
            if( nNoPage > nPage) break;
        }

    }catch( Exception &e)
    {
        if( !dynamic_cast<ELastError*>(&e) ||
            e.Message != ELastError::GetSysErrorMsg( ERROR_HANDLE_EOF) )
            EICDisplayException( TAG(150), e);
    }
    }__finally
    {
        ASSERT( !m_ListOfPushedFont->Count);
        delete m_ListOfPushedFont;
        delete m_ListOfPushedSizeDeciPoint;

        CloseHandle( m_hCerfaFile);
        m_hCerfaFile = 0;
        SetFont( NULL);
        m_Canvas->Font->Color = (TColor)0;  m_Canvas->Font->Color = OldFont->Color;
        m_Canvas->Font->Height = 0;         m_Canvas->Font->Height = OldFont->Height;
        m_Canvas->Font->Name = "";          m_Canvas->Font->Name = OldFont->Name;
        m_Canvas->Font->Style.Clear();      m_Canvas->Font->Style = OldFont->Style;
        ::_DeleteDC( TAGL, m_hDCRefCerfa);
        delete OldFont;
    }
}

void __fastcall TCerfa::VLine( int x, int yTop, int yBottom)
{
  try{
    ASSERT( yBottom >= yTop);
//    m_Canvas->Brush->Color = clBlack;
//    m_Canvas->Brush->Style = bsSolid;
    m_Canvas->Pen->Color = clBlack;
    m_Canvas->Pen->Style = psSolid;
    m_Canvas->Pen->Width = 1;

    ::_MoveToEx(TAGL, m_Canvas->Handle, x = CX( x), CY( yTop), NULL);
    ::_LineTo(TAGL, m_Canvas->Handle, x, CY( yBottom) + Glob->nRectifPourDessin);
  }CATCH_EXCEPTION( TAGL);
}


/*----------------------------------------------------------------------------------------------------------------------------*/
void __fastcall TCerfa::HLine( int y, int xLeft, int xRight)
{
  try{
    ASSERT( xRight >= xLeft);
//    m_Canvas->Brush->Color = clBlack;
//    m_Canvas->Brush->Style = bsSolid;
    m_Canvas->Pen->Color = clBlack;
    m_Canvas->Pen->Style = psSolid;
    m_Canvas->Pen->Width = 1;

    ::_MoveToEx(TAGL, m_Canvas->Handle, CX( xLeft), y = CY( y), NULL);
    ::_LineTo(TAGL, m_Canvas->Handle, CX( xRight) + Glob->nRectifPourDessin, y);
  }CATCH_EXCEPTION( TAGL);
}


/*----------------------------------------------------------------------------------------------------------------------------*/
void __fastcall TCerfa::Cadre( int xLeft, int yTop, int xRight, int yBottom)
{
  try{
    int xDeb = 0, xFin = 0;

    if(xLeft < 0)
        xLeft = TCoordonnee::Pixels2DixMmX( m_xDroit - m_Gauche);

    VLine( (xLeft < 0) ? xDeb : xLeft, yTop, yBottom );
    VLine( (xLeft < 0) ? xFin : xRight, yTop, yBottom );
    HLine( yTop, (xLeft < 0) ? xDeb : xLeft, (xLeft < 0) ? xFin : xRight );
    HLine( yBottom/*+Glob->nRectifPourDessin*/, (xLeft < 0) ? xDeb : xLeft, (xLeft < 0) ? xFin : xRight );
  }CATCH_EXCEPTION( TAGL);
}


/*----------------------------------------------------------------------------------------------------------------------------*/
void __fastcall TCerfa::Rectangle( int xLeft, int yTop, int xRight, int yBottom,
                                   enum GRISE_DE_RECTANGLE eGrise)
{
  try{
	COLORREF crColor;

    ASSERT( ( eGrise >= 0) && ( eGrise <= 100));


    //if( m_bTrueColors)
    switch( eGrise)
    {
            AFFECTE_COLOR( crColor);
            default             :
                {
                    int      nGray = ( ( 100 - eGrise) * 255) / 100;
                    crColor = (TColor)RGB( nGray, nGray, nGray);
                }

    }
    m_Canvas->Brush->Color = (TColor)crColor;

    RECT rc;
    rc.left = CX( xLeft);
    rc.top = CY( yTop);
    rc.right = CX( xRight) + Glob->nRectifPourDessin;
    rc.bottom = CY( yBottom) + Glob->nRectifPourDessin;

    ::_FillRect( TAGL, m_Canvas->Handle, &rc, m_Canvas->Brush->Handle);
  }CATCH_EXCEPTION( TAGL);
}

/*************************************************************************\
************* FlecheOut ***************************************************
***************************************************************************
*                                                                         *
*    Cette procédure permet de tracer une flèche horizontale vers la      *
* droite ou la gauche.                                                    *
*                                                                         *
* Paramètres :                                                            *
*  xDepart, yDepart : Origine, en mm/10, du 'manche' de la flèche.        *
*  nLongueurTotale  : Longueur totale de la flèche (<0 pour dessiner une  *
*                     flèche dirigée vers la gauche)                      *
*  nLargeurBout     : Largeur du bout de la flèche (<0 pour gauche)       *
*  nDemiHauteurBout : Demi-hauteur du bout de la flèche (tjs > 0)         *
*                                                                         *
* Note : abs( nLongueurTotale) doit être > 0, de même signe que           *
*        nLargeurBout et >= à abs( nLargeurBout).                         *
\*************************************************************************/
void __fastcall TCerfa::FlecheOut( int xDepart,
                           int yDepart,
                           int nLongueurTotale,
                           int nLargeurBout,
                           int nDemiHauteurBout)
{
  try{
    TBrush* pOldBrush = new TBrush();
    TPoint ptSommetsBouts[ 4] = { TPoint( CX( xDepart + nLongueurTotale),                CY( yDepart)),
                                  TPoint( CX( xDepart + nLongueurTotale - nLargeurBout), CY( yDepart - nDemiHauteurBout)),
                                  TPoint( CX( xDepart + nLongueurTotale - nLargeurBout), CY( yDepart + nDemiHauteurBout)),
                                  TPoint( CX( xDepart + nLongueurTotale),                CY( yDepart))};


    ////////////////////
    // Pré-conditions //
    ////////////////////
    ASSERT( ( xDepart >= 0) && ( yDepart >= 0));
    ASSERT( ( ( nLongueurTotale > 0) && ( nLargeurBout > 0) && ( nLongueurTotale >= nLargeurBout)) || /* Flèche vers la droite */
         ( ( nLongueurTotale < 0) && ( nLargeurBout < 0) && ( nLongueurTotale <= nLargeurBout)));  /* Flèche vers la gauche */
    ASSERT( nDemiHauteurBout > 0);

    HLine( yDepart, xDepart, xDepart + nLongueurTotale - nLargeurBout);

    pOldBrush->Assign( m_Canvas->Brush);
    m_Canvas->Brush->Color = clBlack;
    m_Canvas->Brush->Style = bsSolid;
    MyPolygon( m_Canvas, ptSommetsBouts, 4);
    m_Canvas->Brush->Assign( pOldBrush);
    delete pOldBrush;
  }CATCH_EXCEPTION( TAGL);
} /* CDCDevice::FlecheOut() */

void __fastcall TCerfa::Panneau( int xLeft, int yTop, int xRightAvantFleche, int yBottom,
                                 int nLongueurFleche)
{
  try{
    TPoint ptSommetsPanneau[ 6] = { TPoint( CX( xLeft),                               CY( yTop)),
                                    TPoint( CX( xRightAvantFleche),                   CY( yTop)),
                                    TPoint( CX( xRightAvantFleche + nLongueurFleche), CY( ( yTop + yBottom ) / 2)),
                                    TPoint( CX( xRightAvantFleche),                   CY( yBottom)),
                                    TPoint( CX( xLeft),                               CY( yBottom)),
                                    TPoint( CX( xLeft),                               CY( yTop))};


    ////////////////////
    // Pré-conditions //
    ////////////////////
    ASSERT( ( xLeft >= 0) && ( yTop >= 0) && ( nLongueurFleche > 0));

    MyPolygon( m_Canvas, ptSommetsPanneau, 6);
  }CATCH_EXCEPTION( TAGL);
} /* CDCDevice::Panneau() */


void __fastcall TCerfa::ReadFields()
{
  try{
    short sh;
    ReadFile( &sh, sizeof( sh));
    if( sh)
    {
        FIELD_DESC* m_pfdFields = new FIELD_DESC[ sh];
        VERIFY( m_pfdFields != NULL);
        ReadFile( m_pfdFields, sh * sizeof( FIELD_DESC));
        delete[] m_pfdFields;
    }
  }CATCH_EXCEPTION( TAGL);
}
int  __fastcall TCerfa::CX( int nDixMm)
{
  try{
    int nTwips = TCoordonnee::DixMm2Twips( nDixMm) + m_Rc.Left.Twips - m_PtOrigin.x.Twips;
    if( IsPrinting())
        nTwips -= TCoordonnee::Pixels2TwipsX( Glob->EICPrinter->PhysicalOffsetX);
    return TCoordonnee::Twips2PixelsX( nTwips);
  }CATCH_EXCEPTION( TAGL);
  return 0;
}
int  __fastcall TCerfa::CY( int nDixMm)
{
  try{
    int nTwips = TCoordonnee::DixMm2Twips( nDixMm)*COEF_Y + m_Rc.Top.Twips - m_PtOrigin.y.Twips;
    if( IsPrinting())
        nTwips -= TCoordonnee::Pixels2TwipsY( Glob->EICPrinter->PhysicalOffsetY);
    return TCoordonnee::Twips2PixelsY( nTwips);
  }CATCH_EXCEPTION( TAGL);
  return 0;
}

void __fastcall TCerfa::TextOutReduit( int        x,
                               int        y,
                               const char *pcszTxt,
                               int Echelle)
{
  try{
    if( RESOL_REF_CERFA == 300)
    {
        PushFont();
        FontSize((int)floor(m_nLastSizeDeciPoint * (double)Echelle / 100.0));
    }
    TextOut( x, y, pcszTxt);
    if( RESOL_REF_CERFA == 300)
        PopFont();
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::CapitalOut
(
  int        x,           // Coordonnée, en mm/10
  int        y,           // ''
  int        nUpSize,     // Taille des grandes capitales
  int        nLoSize,     // Taille des petites capitales
  const char *pcszTxt     // chaine
)
{
  try{
    int    bIsUpper;
    char   szTmp[ 2] = "?";
    SIZE   Size;

    ASSERT( pcszTxt);

    TBrushStyle bs = m_Canvas->Brush->Style;
    m_Canvas->Brush->Style = bsClear;

    x = CX( x);
    y = CY( y);

    PushFont();
    while( ( *szTmp = *pcszTxt++) != '\0')
    {
        switch( *szTmp)
        {
            case 'É' : bIsUpper = TRUE;                   break;
            default  : bIsUpper = ::IsCharUpper( *szTmp); break;
        }

        {
          // Changement de la taille de la fonte courante
            FontSize( bIsUpper ? nUpSize : nLoSize);//nNewFontSize);
        }

        // Passage en majuscule
        switch( *szTmp)
        {   case 'é' : *szTmp = 'É';               break;
            case 'à' : *szTmp = 'À';               break;
            case 'è' : *szTmp = 'È';               break;
            case 'ô' : *szTmp = 'Ô';               break;
            default  : ::AnsiUpperBuff( szTmp, 1); break;
        }

        // Sortie du caractère
        RECT r; r.left= x-1; r.top = y-20; r.right = x+Size.cx; r.bottom = y+20;
        ::_ExtTextOut( TAGL, m_Canvas->Handle, x, y, 0, &r, szTmp, strlen( szTmp), NULL);

        // Ajout de la largeur du caractère pour x
        ::_GetTextExtentPoint32( TAGL, m_Canvas->Handle, szTmp, 1, &Size);
        x += Size.cx;
    }
    m_xDroit = x;

    PopFont();

    m_Canvas->Brush->Style = bs;
    
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::TextOut
(
  int        x,         // Coordonnée, en mm/10
  int        y,         // ''
  const char *pcszTxt   // chaine
)
{
  try{
    int    xDeb, nNbEspaces = CompteEspaces( pcszTxt), xSortie, ySortie;
    TCanvas *OldCanvas = m_Canvas;
    Graphics::TBitmap *Bmp = NULL;
    HFONT hFont = 0, hOldFont = 0;

    TBrushStyle bs = m_Canvas->Brush->Style;
    m_Canvas->Brush->Style = bsClear;

    /** 25/03/98 par moi **/
    xDeb = (x < 0) ? m_xDroit : CX(x);

    int *lpDx = NULL;
    SIZE SizePrinter, SizeEcran;
    CalculSizeSupport(m_Canvas, RESOL_REF_CERFA, pcszTxt, lpDx, SizePrinter);
    ::_GetTextExtentPoint32( TAGL, m_hDCRefCerfa, pcszTxt, strlen( pcszTxt), &SizeEcran);

    xSortie = xDeb; ySortie = CY( y);
    if( m_lf.lfOrientation && IsPrinting() && !Glob->EICPrinter->PDF && Glob->SortieTextesVerticauxParBmp)
//                                           && ( !(Glob->EICPrinter->GetDeviceCaps( TEXTCAPS) & TC_CR_ANY) || Glob->SortieTextesVerticauxParBmp) )
    {
        LOGFONT lf = m_lf;
        if( lpDx) { delete []lpDx; lpDx = NULL;}
        GetFont( &lf);
        TRect RcApresBmp = TRect(0, 0, ::MulDiv( SizeEcran.cy, Glob->ResolutionX, RESOL_REF_CERFA),
                                       ::MulDiv( SizeEcran.cx, Glob->ResolutionY, RESOL_REF_CERFA));
        CreationBitmapIntermediaire( RcApresBmp, RcApresBmp, m_Canvas, Bmp, (TColor)::_GetTextColor( TAGL, m_Canvas->Handle));
        m_Canvas = Bmp->Canvas;

        hFont         = ::_CreateFontIndirect( TAGL, &lf);
        hOldFont      = ::_SelectObject( TAGL, m_Canvas->Handle, m_hFont);
        CalculSizeSupport(m_Canvas, RESOL_REF_CERFA, pcszTxt, lpDx, SizePrinter);
        xSortie = 0; ySortie = RcApresBmp.bottom;
    }

    int cx = AjustFontSize( pcszTxt, nNbEspaces, m_Canvas, SizePrinter, RESOL_REF_CERFA, RESOL_REF_CERFA, SizeEcran.cx, false, lpDx);

    RECT r; r.left= xDeb-1; r.top = CY( y)-20; r.right = xDeb+cx; r.bottom = CY( y)+20;
    ::_ExtTextOut( TAGL, m_Canvas->Handle, xSortie, ySortie, 0, &r, pcszTxt, strlen( pcszTxt), lpDx);
    m_xDroit = cx + xDeb;

    if( Bmp)
    {
        ::_SelectObject( TAGL, m_Canvas->Handle, hOldFont); hOldFont = 0;
        if(hFont) { ::_DeleteObject( TAGL, hFont);          hFont = 0;}

        m_Canvas = OldCanvas;
        DrawTransparentBitmap( m_Canvas->Handle, Bmp, xDeb-Bmp->Width,
                                   CY( y)-Bmp->Height, ColorToRGB(Bmp->TransparentColor));
        delete Bmp;
    }
    m_Canvas->Brush->Style = bs;

    if( lpDx) { delete []lpDx; lpDx = NULL;}
  }CATCH_EXCEPTION(TAGL);
}

void __fastcall TCerfa::GetFont( LOGFONT *lf)
{
  try{
    memset( lf, 0, sizeof( lf));
    HFONT h = ::_GetCurrentObject( TAGL, m_Canvas->Handle, OBJ_FONT);
    ::_GetObject( TAGL, h, sizeof (LOGFONT), lf);
	  lf->lfCharSet        = ANSI_CHARSET;
  	lf->lfOutPrecision   = OUT_DEFAULT_PRECIS;
	  lf->lfClipPrecision  = CLIP_DEFAULT_PRECIS;
  	lf->lfQuality        = PROOF_QUALITY;
	  lf->lfPitchAndFamily = DEFAULT_PITCH;
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::SetFont( const LOGFONT *lf)
{
EICTag l=TAGL;
    try{
        if( m_hOldFont)
        {
l=TAGL;            ::_SelectObject( TAGL, m_Canvas->Handle, m_hOldFont);    m_hOldFont = 0;
l=TAGL;            if(m_hFont) { ::_DeleteObject( TAGL, m_hFont);            m_hFont = 0;}
        }
    }CATCH_EXCEPTION(l);
    try{
        if( m_hOldFontEcran)
        {
l=TAGL;            ::_SelectObject( TAGL, m_hDCRefCerfa, m_hOldFontEcran);    m_hOldFontEcran = 0;
l=TAGL;            if(m_hFontEcran) { ::_DeleteObject( TAGL, m_hFontEcran);      m_hFontEcran = 0;}
        }
    }CATCH_EXCEPTION(l);
    try{
        if( lf)
        {
l=TAGL;            m_hFont         = ::_CreateFontIndirect( TAGL, lf);
l=TAGL;            m_hOldFont      = ::_SelectObject( TAGL, m_Canvas->Handle, m_hFont);
                   LOGFONT lfEcran = *lf;
	lfEcran.lfCharSet        = ANSI_CHARSET;
	lfEcran.lfOutPrecision   = OUT_DEFAULT_PRECIS;
	lfEcran.lfClipPrecision  = CLIP_DEFAULT_PRECIS;
	lfEcran.lfQuality        = PROOF_QUALITY;
	lfEcran.lfPitchAndFamily = DEFAULT_PITCH;
#if RESOL_REF_CERFA == 1440
                   lfEcran.lfHeight = long(- 1.97 * m_nLastSizeDeciPoint);
#else
                   lfEcran.lfHeight = -floor((double)m_nLastSizeDeciPoint* RESOL_REF_CERFA/ 720.);
#endif
l=TAGL;            m_hFontEcran    = ::_CreateFontIndirect( TAGL, &lfEcran);
l=TAGL;            m_hOldFontEcran = ::_SelectObject( TAGL, m_hDCRefCerfa,   m_hFontEcran);
        }
    }CATCH_EXCEPTION(l);
}

void __fastcall TCerfa::FontStyle( enum FONT_STYLE eStyle)
{
  try{
    GetFont( &m_lf);
    #if FS_MAX != 2
        #error "style de fonte non reconnue"
    #endif
    switch( eStyle )
    {
        case FS_COURIER : strcpy( m_lf.lfFaceName, "Courier New");     break;
        case FS_UNIVERS : strcpy( m_lf.lfFaceName, "Arial");           break;
        case FS_CGTIMES : strcpy( m_lf.lfFaceName, "Times New Roman"); break;
//        case FS_COURIER : strcpy( lf.lfFaceName, "Courier New");     break;
//        case FS_UNIVERS : strcpy( lf.lfFaceName, "Arial");           break;
//        case FS_CGTIMES : strcpy( lf.lfFaceName, "Times New Roman"); break;
        default         : ASSERT( FALSE);
    }
    SetFont( &m_lf);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::FontItalic( int bItalic /* = TRUE */)
{
  try{
    GetFont( &m_lf);
    m_lf.lfItalic         = (BYTE)bItalic;
    SetFont( &m_lf);
  }CATCH_EXCEPTION( TAGL);
}

//---------------------------------------------------------------------------------
//
// Change la graisse de la fonte courante
//

void __fastcall TCerfa::FontWeight( int eWeight)
{
  try{
    GetFont( &m_lf);
    m_lf.lfWeight         = eWeight;

    SetFont( &m_lf);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::FontSize( int nSizeDeciPoint)
{
  try{
    ASSERT( nSizeDeciPoint > 0);
    m_nLastSizeDeciPoint = nSizeDeciPoint;
    GetFont( &m_lf);
    m_lf.lfQuality        = PROOF_QUALITY;
    m_lf.lfHeight         = -floor((double)nSizeDeciPoint* (double)Glob->ResolutionY / (double)720);

    SetFont( &m_lf);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::FontOrientation( int Orientation)
{
  try{
    GetFont( &m_lf);
    m_lf.lfOrientation	= Orientation;
    m_lf.lfEscapement 	= Orientation;
    m_lf.lfOutPrecision 	= OUT_TT_ONLY_PRECIS;
    m_lf.lfCharSet      	= ANSI_CHARSET;
    SetFont( &m_lf);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::WhiteText()
{
  try{
    ::_SetTextColor( TAGL, m_Canvas->Handle, RGB( 255, 255, 255));
  }CATCH_EXCEPTION( TAGL);
}


void __fastcall TCerfa::BlackText()
{
  try{
    if( !IsPrinting( ))
        ::_SetTextColor( TAGL, m_Canvas->Handle, RGB( 0, 0, 128));
    else{
//        ::_SetTextColor( TAGL, m_Canvas->Handle, RGB( 40, 25, 30));
        ::_SetTextColor( TAGL, m_Canvas->Handle, RGB( 0, 0, 0));
    }
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::ColorTextAndBk(enum GRISE_DE_RECTANGLE eTextColor, enum GRISE_DE_RECTANGLE eBkColor)
{
  try{
    int nGrayText   = ( ( 100 - eTextColor) * 255) / 100;
    int nGrayBk     = ( ( 100 - eBkColor) * 255) / 100;

    ::_SetTextColor( TAGL, m_Canvas->Handle, RGB( nGrayText, nGrayText, nGrayText));
    ::_SetBkColor( TAGL, m_Canvas->Handle, RGB( nGrayBk, nGrayBk, nGrayBk));
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::TextOutV( int xBottom, int yLeft, const char *pcszTxt)
{
  try{
    FontOrientation(900);
    TextOut(xBottom, yLeft, pcszTxt);
    FontOrientation(0);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::TextOutBiais( int xBottom, int yLeft, const char *pcszTxt)
{
  try{
    FontOrientation(450);
    TextOut(xBottom, yLeft, pcszTxt);
    FontOrientation(0);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::PushFont( void)
{
  try{
    LOGFONT *PushedFont = new LOGFONT( );
    GetFont( PushedFont);
    TRACEF( TAGL, "PushFont [%p]", PushedFont);
    m_ListOfPushedFont->Add( PushedFont);
    m_ListOfPushedSizeDeciPoint->Add( (void*)m_nLastSizeDeciPoint);
  }CATCH_EXCEPTION( TAGL);
} /* CDevice::PushFont() */

void __fastcall TCerfa::PopFont( void)
{
  try{
    LOGFONT *PushedFont = (LOGFONT*)m_ListOfPushedFont->Last( );
    m_ListOfPushedFont->Delete( m_ListOfPushedFont->Count - 1);
TRACEF( TAGL, "PopFont [%p]", PushedFont);
    SetFont( PushedFont);
    delete PushedFont;
    m_nLastSizeDeciPoint = (int)m_ListOfPushedSizeDeciPoint->Last( );
    FontSize( (int)m_ListOfPushedSizeDeciPoint->Last( ));
    m_ListOfPushedSizeDeciPoint->Delete( m_ListOfPushedSizeDeciPoint->Count - 1);
  }CATCH_EXCEPTION( TAGL);
} /* CDevice::PopFont() */

void CALLBACK DrawDots(int xPos, int yPos, LPARAM lpData )
{
    static short cSpaces = 1;
  try{

    if (cSpaces == 2) {
        SetPixel((HDC)lpData, xPos, yPos, 0);
        cSpaces = 1;
    }
    else
    {
       cSpaces++;
       //SetPixel((HDC)lpData, xPos, yPos, RGB(255,255,255));
       SetPixel((HDC)lpData, xPos, yPos, 0);
    }
  }CATCH_EXCEPTION( TAGL);
}


void  __fastcall TCerfa::DottedLine
(
  int y,                                            // Coordonnées, en mm/10, de la ligne à tracer
  int xLeft,                                        // ''
  int xRight,                                       // ''
  enum DOTTED_LINE_STYLE eStyle /* = DLS_DEFAULT */ // Style de la ligne à tracer (DLS_*)
)
{
  try{
    int nTmp = xLeft;
    int xDroitFutur;

    if(xLeft < 0)
        xLeft = TCoordonnee::Pixels2DixMmX( m_xDroit - m_Gauche);

    //On inverse les valeurs de xLeft et xRight
    //En fonction du point de départ, car on vas toujours de droite à gauche
    if (xLeft > xRight) {
        nTmp    = xLeft;
        xLeft   = xRight;
        xRight  = nTmp;
    }

    if( eStyle == DLS_TIRETS)
    {   /***********************************************************/
        /*** Edition d'une ligne de tirets de 2mm espacés de 1mm ***/
        /***********************************************************/
        /* xLeft = Abscisse gauche du prochain tiret à éditer */
        /* nTmp  = Abscisse droite du prochain tiret à éditer */
        nTmp = xLeft;
        while( xRight > ( xLeft += 10))
        {
            if( ( nTmp += 30) > xRight)
                nTmp = xRight;
            HLine( y, xLeft, nTmp);
            xLeft = nTmp;
        }
        m_xDroit = CX(xRight);
        return;
    }


//    PushFont();
    /************************************************************/
    /*** Sélection de la fonte d'édition d'une ligne stylisée ***/
    /************************************************************/
//    FontSize( 40);
//    FontWeight( FW_THIN);

    /**************************************************/
    /*** Initialisations pour l'édition de la ligne ***/
    /**************************************************/
    /* nTmp = Espace entre 2 points de la ligne (en décimillimètres) */
    #if DLS_MAX != 2
        #error "style de tiret non reconnu"
    #endif
    switch( eStyle)
    {   default          : ASSERT( FALSE);
//        case DLS_DEFAULT :
        case DLS_ESPACES : nTmp = 12; break;  // Points espacés ( 16 pour 2 cm )
        case DLS_SERRES  : nTmp =  5; break;  // Points serrés  (  2 pour 1 mm )
    }


    /***************************/
    /*** Edition de la ligne ***/
    /***************************/
    xDroitFutur = CX(xRight);
    while( xRight > xLeft)
    { //TextOut( xRight, y, ".");
        HLine(y, xRight-1, xRight);
        xRight -= nTmp;
    }

    m_xDroit = xDroitFutur;
//    PopFont();
  }CATCH_EXCEPTION( TAGL);
}

void  __fastcall TCerfa::PastilleOut( int         xLeft,
                             int         yBaseText,
                             const char* pcszText,
                             int         bIsBlack /* = FALSE */)
{
  try{
    TBrush* pOldBrush = new TBrush();
    int     xDeb, xFin, xLarg, yDeb;
    SIZE    cSize;

    ASSERT( yBaseText >= 28);
    ASSERT( pcszText && ( strlen( pcszText) <= 3));

    // Initialisons la valeur du coin inférieur gauche du rectangle contenant l'ellipse
    xDeb = (xLeft < 0) ? m_xDroit : CX(xLeft);

    PushFont();


    pOldBrush->Assign( m_Canvas->Brush);
    m_Canvas->Brush->Color = bIsBlack ? clBlack : clWhite;
    xLarg = CX( 33) - m_Gauche + 1;
    xFin = CX( TCoordonnee::Pixels2DixMmX( xDeb - m_Gauche) + 33);
    yDeb = CY( yBaseText - 28);
    ::_Ellipse( TAGL, m_Canvas->Handle, xDeb, yDeb, xDeb + xLarg, yDeb + xLarg-1);


//    m_xDroit = xDeb;// + ( ( strlen( pcszText) == 1) ? 9 : 1);

    ::_GetTextExtentPoint32( TAGL, m_Canvas->Handle, pcszText, strlen(pcszText), &cSize);
    xDeb += (xFin - xDeb - cSize.cx) / 2 ;

    if( bIsBlack) WhiteText();
    FontStyle( FS_UNIVERS);
    FontSize( 64);
    TextOut( TCoordonnee::Pixels2DixMmX( xDeb/* + ( ( strlen( pcszText) == 1) ? 9 : 1)*/- m_Gauche),
             yBaseText-2, pcszText);
    if( bIsBlack) BlackText();

    m_Canvas->Brush->Assign( pOldBrush);
    delete pOldBrush;

    m_xDroit = xFin;

    PopFont();

  }CATCH_EXCEPTION( TAGL);
} /* CDCDevice::PastilleOut() */

void __fastcall TCerfa::BmpOut
(
  int xLeft,        // X
  int yTop,         // Y
  int NbOctetsLarg, // Nombre d'octets occupés par une ligne du bitmap
  int NbLignes      // Nombre de lignes du bitmap
)
{
  try{
    RGBQUAD argbq[] = {{ 255, 255, 255, 0 },{ 0, 0, 0, 0 }};
    MyBmpOut( xLeft, yTop, NbOctetsLarg, NbLignes, argbq, SRCAND);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::BmpOutInverse
(
  int xLeft,        // X
  int yTop,         // Y
  int NbOctetsLarg, // Nombre d'octets occupés par une ligne du bitmap
  int NbLignes      // Nombre de lignes du bitmap
)
{
  try{
    RGBQUAD argbq[] = {{ 0, 0, 0, 0 },{ 255, 255, 255, 0 }};
    MyBmpOut( xLeft, yTop, NbOctetsLarg, NbLignes, argbq, SRCAND);
    //MyBmpOut( xLeft, yTop, NbOctetsLarg, NbLignes, argbq, NOTSRCCOPY);
  }CATCH_EXCEPTION( TAGL);
}


void __fastcall TCerfa::BmpOutGrisDur
(
  int xLeft,        // X
  int yTop,         // Y
  int NbOctetsLarg, // Nombre d'octets occupés par une ligne du bitmap
  int NbLignes      // Nombre de lignes du bitmap
)
{
  try{
    RGBQUAD argbq[] = {{ 255, 255, 255, 0 },{ 204, 204, 204, 0 }};
    MyBmpOut( xLeft, yTop, NbOctetsLarg, NbLignes,
              argbq, SRCAND);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::BmpOutEx
(
  int xLeft,        // X
  int yTop,         // Y
  int NbOctetsLarg, // Nombre d'octets occupés par une ligne du bitmap
  int NbLignes,      // Nombre de lignes du bitmap
  COLORREF cr       // Couleur du ForeGround
)
{
  try{
 	char cRed   = GetRValue(cr);
 	char cGreen = GetGValue(cr);
 	char cBlue  = GetBValue(cr);

 	RGBQUAD argbq[] = {{ 255, 255, 255, 0 },{ cRed, cGreen, cBlue, 0 }};
    MyBmpOut( xLeft, yTop, NbOctetsLarg, NbLignes, argbq, SRCAND);
  }CATCH_EXCEPTION( TAGL);
}

void __fastcall TCerfa::MyBmpOut
(
  int xLeft,        // X
  int yTop,         // Y
  int NbOctetsLarg, // Nombre d'octets occupés par une ligne du bitmap
  int NbLignes,      // Nombre de lignes du bitmap
  RGBQUAD argbq[],
  int Op
)
{
  try{
    LPBITMAPINFO pbmi = (LPBITMAPINFO) ::_GlobalAlloc(TAGL, GPTR, sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * 2));

    if (pbmi == NULL)  return;

    pbmi->bmiHeader.biSize   = sizeof(BITMAPINFOHEADER);
    pbmi->bmiHeader.biWidth  = NbOctetsLarg * 8;
    pbmi->bmiHeader.biHeight = NbLignes;
    pbmi->bmiHeader.biPlanes  = 1;

    pbmi->bmiHeader.biBitCount = 1;
    pbmi->bmiHeader.biCompression = BI_RGB;

    memcpy(pbmi->bmiColors, argbq, sizeof(RGBQUAD) * 2);

    // Les lignes d'un bitmap sous située sur des frontières de 4 octets
    int NbOctetPerLine  = ((NbOctetsLarg+3)/4)*4;

    // Taille des bits de l'images
    int nBitmapSize  = NbOctetPerLine *  NbLignes;

    char *aBits = (char *)::_GlobalAlloc(TAGL, GPTR, nBitmapSize);
    if (aBits == NULL)
    {
        ::_GlobalFree(TAGL, pbmi);
        return;
    }

    // Lecture des lignes de l'image
    for ( int i=0; i<NbLignes;i++)
    {
   		unsigned uDepl = (unsigned)NbOctetPerLine * ((unsigned)NbLignes - (unsigned)1 - i);
  		ReadFile( (void*)(aBits+uDepl), NbOctetsLarg);
    }

    // Sortie du bitmap
    int cxDIB = (int)pbmi->bmiHeader.biWidth;
    int cyDIB = (int)pbmi->bmiHeader.biHeight;
    int cdstxDIB = CX(TCoordonnee::Pixels2DixMm( cxDIB, 300))-m_Gauche-1;
    int cdstyDIB = TCoordonnee::Twips2PixelsY(TCoordonnee::Pixels2Twips( cyDIB* COEF_Y, 300));//CY(TCoordonnee::Pixels2DixMm( cyDIB, 300))-m_Rc.Top-1;

    MyStretchDIBits( m_Canvas,
                     CX(xLeft), CY(yTop), cdstxDIB, cdstyDIB,
                     0, 0, cxDIB, cyDIB,
                     (const void FAR*)aBits,
                     (LPBITMAPINFO) pbmi,
                     DIB_RGB_COLORS,
                     Op);

    ::_GlobalFree(TAGL, aBits);

    ::_GlobalFree(TAGL, pbmi);
  }CATCH_EXCEPTION( TAGL);
}


void __fastcall TCerfa::CaseACocher( int         xLeft,
                                     int         yBaseLine,
                                     const char* pcszTextInto /* = NULL */)
{
  try{
    if(xLeft < 0)
        xLeft = (int)(((double)m_xDroit * 2.54 - 0.5) / 3.0 - 50.0);

    if( pcszTextInto)
    {
        Rectangle( xLeft, yBaseLine - 25, xLeft + 28, yBaseLine + 3, M_ROUGE);
        WhiteText();
        TextOut( xLeft + 5, yBaseLine, pcszTextInto);
        BlackText();
    }

    // Cadre de la case à cocher
    Cadre( xLeft, yBaseLine - 25, xLeft + 28, yBaseLine + 3);
    m_xDroit = CX(xLeft + 28);
  }CATCH_EXCEPTION( TAGL);
} /* CDevice::CaseACocher() */


